package com.top.mall.main.goods

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.appbar.AppBarLayout
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.app.BaseApplication
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.GoodsDetails
import com.top.mall.jetpack.vm.CouponViewModel
import com.top.mall.jetpack.vm.GoodsViewModel
import com.top.mall.main.cart.CartActivity
import com.top.mall.utils.SpanUtils
import kotlinx.android.synthetic.main.activity_goods_details.*
import kotlinx.android.synthetic.main.activity_goods_details.imageViewBack
import kotlinx.android.synthetic.main.include_goods_details_body.*
import kotlinx.android.synthetic.main.include_goods_details_head.*

class GoodsDetailsActivity : OwnerViewModeActivity<GoodsViewModel>(),
    AppBarLayout.OnOffsetChangedListener {

    companion object {

        private const val KEY_GOODS_ID = "KEY_GOODS_ID"
        fun open(goodsId: Int) {
            val bundle = Bundle().apply {
                putInt(KEY_GOODS_ID, goodsId)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }

    private val mGoodsId: Int
        get() = intent?.extras?.getInt(KEY_GOODS_ID, 0) ?: 0

    override fun viewModel(): GoodsViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_goods_details

    override fun stateBar(): StatusBar {
        return StatusBar(view = clGoodsDetailsBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }
        BarUtils.setStatusBarColor(this, Color.WHITE)
        //为AppBar设置滑动监听
        appBarGoodsDetails.addOnOffsetChangedListener(this)


        rbGoodsDetailsTab1.setOnClickListener {
            appBarGoodsDetails.setExpanded(true)
        }
        rbGoodsDetailsTab2.setOnClickListener {
            appBarGoodsDetails.setExpanded(false)
        }

        tvGoodsDetailsJoinCart.setOnClickListener {
            viewModel.cartAdd(mGoodsId, 1, 0)
        }

        imageView35.setOnClickListener {
            CartActivity.open(activity = this)
        }
    }

    override fun initData() {
        viewModel.goodsInfo(mGoodsId)
    }

    override fun initViewMode() {
        viewModel.goodsDetailsLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.cartAddLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast("加入成功")
        })

        BaseApplication.getCartListLiveData().observe(this, { cartList ->
            text_badge_wait_pay.text = cartList.size.toString()
        })
    }

    private fun fillData(goodsDetails: GoodsDetails) {
        tvGoodsDetailsName.text = goodsDetails.goods_name
        tvGoodsDetailsPrice.text =
            SpanUtils.setMoneySymbol(goodsDetails.price, goodsDetails.active_price)
        ivGoodsDetailsImage.loadCenterImage(if (goodsDetails.images.isEmpty()) "" else goodsDetails.images[0])
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        if (verticalOffset * -1 == appBarGoodsDetails.totalScrollRange) {
            rbGoodsDetailsTab2.isChecked = true
        } else {
            rbGoodsDetailsTab1.isChecked = true
        }
    }

}